package com.wkw.jkyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wkw.jkyf.mapper.BillinfoMapper;
import com.wkw.jkyf.mapper.DruginfoMapper;
import com.wkw.jkyf.pojo.Billinfo;
import com.wkw.jkyf.pojo.Druginfo;
import com.wkw.jkyf.pojo.Problem;
import com.wkw.jkyf.pojo.Saleinfo;
import com.wkw.jkyf.service.IBillinfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
//账单信息
@Service
public class BillinfoServiceImpl  extends ServiceImpl<BillinfoMapper, Billinfo> implements IBillinfoService {

    @Resource
    private BillinfoMapper billinfoMapper;
    @Resource
    private DruginfoMapper druginfoMapper;


    @Override
    public IPage<Billinfo> selectBillinfoPage(int pageNum, int pageSize, String param) {
        QueryWrapper<Billinfo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param)){
            //根据供应商名称精确查询
            queryWrapper.like("sname",param);
        }
        queryWrapper.orderByDesc("id");
        Page<Billinfo> page = new Page<>(pageNum,pageSize);
        return billinfoMapper.selectPage(page,queryWrapper);
    }

    @Override
    @Transactional
    //添加账单信息
    public int addBillinfo(Billinfo billinfo) {
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",billinfo.getDname())
                .eq("number",billinfo.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);
        //本次账单所购买的药品数量
        Integer count = billinfo.getCount();
        druginfo.setStock(druginfo.getStock()+count);
        druginfoMapper.updateById(druginfo);
        //本次所购药品的价格
        Float price = druginfo.getPrice();
        billinfo.setTotal(price*count);
        return billinfoMapper.insert(billinfo);
    }

    @Override
    @Transactional
    public int editBillinfo(Billinfo billinfo) {
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",billinfo.getDname())
                .eq("number",billinfo.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);

        QueryWrapper<Billinfo> billinfoWrapper = new QueryWrapper<>();
        billinfoWrapper
                .eq("id",billinfo.getId());
        //当前数据库的账单信息
        Billinfo oldBillInfo = billinfoMapper.selectOne(billinfoWrapper);
        if(!oldBillInfo.getDnumber().equals(billinfo.getDnumber())||!oldBillInfo.getSname().equals(billinfo.getSname())){
            QueryWrapper<Druginfo> oldDrugWrapper = new QueryWrapper<>();
            oldDrugWrapper
                    .eq("name",oldBillInfo.getDname())
                    .eq("number",oldBillInfo.getDnumber());
            Druginfo oldDrugInfo = druginfoMapper.selectOne(oldDrugWrapper);
            //药品数量
             Integer stock = oldDrugInfo.getStock();
             //从供应商进货的药品数量
             Integer count = oldBillInfo.getCount();
             if(count>stock){
                 return -1;
             }
             oldDrugInfo.setStock(stock-count);
             druginfoMapper.updateById(oldDrugInfo);
             druginfo.setStock(billinfo.getCount()+druginfo.getStock());
             druginfoMapper.updateById(druginfo);
             billinfoMapper.updateById(billinfo);
             return 1;
        }
        //当前数据库的供应商药品数量
        int oldCount = oldBillInfo.getCount();
        //新的供应商药品数量
        int newCount = billinfo.getCount();
        if(newCount>oldCount){
            druginfo.setStock(druginfo.getStock()+(newCount-oldCount));
            druginfoMapper.updateById(druginfo);
        }else if(newCount<oldCount){
            int newSaleCount = oldCount - newCount;
            if(newSaleCount>druginfo.getStock()){
                return -1;
            }
            druginfo.setStock(druginfo.getStock()-newSaleCount);
            druginfoMapper.updateById(druginfo);
        }
        return billinfoMapper.updateById(billinfo);
    }

    @Override
    public Billinfo queryBillinfoById(Integer id) {
        return billinfoMapper.selectById(id);
    }

    @Override
    @Transactional
    public int deleteBillinfoById(Integer id) {
        Billinfo billinfo = billinfoMapper.selectById(id);
        //查询出来的数量
        Integer count = billinfo.getCount();
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",billinfo.getDname())
                .eq("number",billinfo.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);
        if(count>druginfo.getStock()){
            return -1;
        }
        druginfo.setStock(druginfo.getStock()-count);
        druginfoMapper.updateById(druginfo);
        return billinfoMapper.deleteById(id);
    }
}
